package com.njth.servlet.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.njth.beans.Category;
import com.njth.beans.Product;
import com.njth.dao.CategoryDAO;
import com.njth.dao.DaoConfig;
import com.njth.dao.ProductDAO;
import com.njth.util.Config;
import com.njth.util.FileAssistant;
import com.njth.util.ScaleImage;

public class AddProduct extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddProduct() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//缩略图高度和宽度
		int h=100;
		int w=150;
		
		ProductDAO pdao=(ProductDAO) DaoConfig.getDao(ProductDAO.class);
		CategoryDAO cdao=(CategoryDAO) DaoConfig.getDao(CategoryDAO.class);
		Product p=new Product();
		String path=Config.getConfigProperty("uploadImagePath");
		File uploadAbsolutDirectory=new File(Config.getContenAbsolutPath(),path);
		Map<String,Object> map=FileAssistant.fileUpload(request, uploadAbsolutDirectory);
		String name=(String)map.get("productname");
		p.setName(name);
		String var=(String)map.get("category");
		Category c=new Category();
		try {
			int id=Integer.parseInt(var);
			c=cdao.getCategory(id);
			p.setCategory(c);
			
			var=(String)map.get("status");
			int s=Integer.parseInt(var);
			p.setStatus(s);
			
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		var=(String)map.get("price");
		try {
			double price=Double.parseDouble(var);
			p.setOprice(price);
			var=(String)map.get("mprice");
			double mp=Double.parseDouble(var);
			p.setSprice(mp);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.setPinfo((String)map.get("info"));
		
		File f1=(File)map.get("file1");
		p.setPpic1(f1.getName());
		String tName1=this.getThumbnailName(f1.getName());
		ScaleImage si=new ScaleImage();
		File f2=(File)map.get("file2");
		p.setPpic2(f2.getName());
		String tName2=this.getThumbnailName(f2.getName());
		try {
			//获取缩略图高度和宽度
			var=Config.getConfigProperty("imgThumbnailHeight");
			h=Integer.parseInt(var);
			var=Config.getConfigProperty("imgThumbnailWidth");
			w=Integer.parseInt(var);
			
			si.saveImageAsJpg(f1.getAbsolutePath(), uploadAbsolutDirectory.getAbsolutePath()+File.separatorChar+tName1, w, h);
			si.saveImageAsJpg(f2.getAbsolutePath(), uploadAbsolutDirectory.getAbsolutePath()+File.separatorChar+tName2, w, h);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		p.setPpci1Thumbnail(tName1);
		p.setPpic2Thumbnail(tName2);
		pdao.insert(p);
		
		//产品保存成功跳转到指定页
		response.sendRedirect(request.getContextPath()+"/com/njth/jsp/product.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	private String getThumbnailName(String name){
		String[] strs = name.split("\\.");
        String tName = "";
        for (int j= 0; j < strs.length; j++) {
            if (j == strs.length - 1) {
                tName = tName + "_t." + strs[j];
            } else {
                tName = tName + strs[j];
            }
        }
        return tName;
	}
}
